package com.camel.base.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.camel.base.entity.BusinessCenterResult;
import com.camel.base.entity.EnumQueryForm;
import com.camel.base.entity.EnumVo;
import com.camel.base.model.BpOrg;
import com.camel.base.model.BpUser;
import com.camel.base.model.TbDict;
import com.camel.base.model.TbDictValue;
import com.camel.base.service.BpOrgService;
import com.camel.base.service.BpUserService;
import com.camel.base.service.TbDictService;
import com.camel.base.service.TbDictValueService;
import com.camel.base.utils.StringUtils;
import com.camel.core.exceptions.CustomException;
import lombok.Data;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.camel.base.entity.BusinessCenterResult.DATA_TAG;

@RestController
public class OtherController {

    @Autowired
    private TbDictValueService<TbDictValue> tbDictValueService;

    @Autowired
    private TbDictService<TbDict> dictService;

    @Autowired
    private BpOrgService<BpOrg> bpOrgService;

    @Autowired
    private BpUserService<BpUser> bpUserService;

    @PostMapping("/baseCenter/model/modelCode/code/list")
    public BusinessCenterResult selectEnumByType(@RequestBody JSONObject params) {
        if (!params.containsKey("codeType")) {
            throw new CustomException("来自模拟中台的提示: 枚举类型不存在");
        }
        String codeType = params.getString("codeType");
        if (StringUtils.isEmpty(codeType)) {
            throw new CustomException("来自模拟中台的提示: 枚举类型[" + codeType + "]不存在");
        }

        List<TbDictValue> dictCode = tbDictValueService.list(new QueryWrapper<TbDictValue>().eq("dict_code", codeType));
        if (CollectionUtils.isEmpty(dictCode)) {
            throw new CustomException("来自模拟中台的提示: 枚举类型[" + codeType + "]所对应的枚举值不存在");
        }

        ArrayList<EnumVo> enumVos = new ArrayList<>();
        for (TbDictValue dictData : dictCode) {
            EnumVo enumVo = new EnumVo();
            enumVo.setCodeId(dictData.getDictCode());
            enumVo.setCodeSortId(String.valueOf(dictData.getId()));
            enumVo.setCodeType(dictData.getDictCode());
            enumVo.setName(dictData.getDictText());
            enumVo.setValue(dictData.getDictVal());
            enumVo.setDispSn(String.valueOf(dictData.getId()));
            enumVo.setContent1(String.valueOf(dictData.getDictText()));
            enumVos.add(enumVo);
        }
        return BusinessCenterResult.success(enumVos);
    }

//    /**
//     * 通过工号获取用户信息
//     *
//     * @param params
//     * @return
//     */
//    @PostMapping("/baseCenter/userOrgService/users/userNo")
//    public Object usersUserNo(@RequestBody JSONObject params) throws SQLException {
//        String userNo = params.getString("userNo");
//        if(StringUtils.isEmpty(userNo)) {
//            throw new CustomException("来自模拟中台的提示: 用户工号不能为空");
//        }
//        BpUser user = bpUserService.getOne(new QueryWrapper<BpUser>().eq("user_name", userNo));
//        if(ObjectUtils.isEmpty(user)) {
//            return BusinessCenterResult.error("来自模拟中台的提示: 用户不存在,请在数据库中添加");
//        }
//        ArrayList<Object> objects = new ArrayList<>();
//        objects.add(user);
//        return BusinessCenterResult.success(objects);
//    }

//    @PostMapping("/baseCenter/userOrgService/user/userId")
//    public BusinessCenterResult queryUserByUserId(@RequestBody Map<String, String> param) {
//        String userId = param.get("userId");
//        if (StringUtils.isEmpty(userId)) {
//            return BusinessCenterResult.error("来自模拟中台的提示: 用户标识ID不能为空");
//        }
//        BpUser bpUser = bpUserService.getById(userId);
//        return BusinessCenterResult.success(bpUser);
//    }

//    @PostMapping("/baseCenter/userOrgService/org/orgNo")
//    public BusinessCenterResult selectEnumByTypeAndValue(@RequestBody Map<String, String> param) {
//        String orgNo = param.get("orgNo");
//        if (StringUtils.isEmpty(orgNo)) {
//            return BusinessCenterResult.error("来自模拟中台的提示: 部门编码不能为空");
//        }
//        BpOrg bpOrg = bpOrgService.getOne(new QueryWrapper<BpOrg>().eq("org_no", orgNo), false);
//        return BusinessCenterResult.success(bpOrg);
//    }

//    /**
//     * 获取部门下的全部用户信息
//     *
//     * @return
//     */
//    @PostMapping("/baseCenter/userOrgService/users/deptNo")
//    public BusinessCenterResult queryDeptAllUserByUserId(@RequestBody Map<String, String> param) {
//        String deptNo = param.get("deptNo");
//        if (StringUtils.isEmpty(deptNo)) {
//            return BusinessCenterResult.error("部门编码不能为空");
//        }
//        List<BpUser> bpUserList = bpUserService.selectBpDeptAllUserByDeptNo(deptNo);
//        bpUserList.removeAll(Collections.singleton(null));
//        return BusinessCenterResult.success(bpUserList);
//    }


//    @PostMapping("/baseCenter/oauth2/accessToken")
//    public BusinessCenterResult accessToken() {
//        StpUtil.login(100 + RandomUtils.nextInt());
//        String tokenValue = StpUtil.getTokenValue();
//        BusinessCenterResult success = BusinessCenterResult.success("操作成功");
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("access_token", tokenValue);
//        jsonObject.put("refresh_token", "c27ea2e34c0e6c337ee44be40c9ae9ad");
//        jsonObject.put("expires_in", "2592000");
//
//        success.put(DATA_TAG, jsonObject);
//        return success;
//
//    }

//    @PostMapping("/baseCenter/oauth2/token/validation")
//    public BusinessCenterResult validation(HttpServletRequest request) {
//        String token = request.getHeader("x-token");
//        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
//        if(ObjectUtils.isEmpty(loginIdByToken)) {
//            return BusinessCenterResult.error("无效的token");
//        }
//        return  BusinessCenterResult.success("操作成功");
//    }

    @Data
    class User {
        public String userId;
        public String userName;
        public String userTitle;
    }


}
